package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Reference
    private VideoApi videoApi;

    @Reference
    private UserInfoApi userInfoApi;

    //视频记录翻页业务
    public ResponseEntity<Object> findByPage(Integer page, Integer pagesize, Long uid) {
        PageResult pageResult = videoApi.findByPage(page, pagesize, uid);
        List<Video> list = (List<Video>) pageResult.getItems();
        ArrayList<VideoVo> videoVos = new ArrayList<>();
        if (list != null) {
            for (Video video : list) {
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(video, videoVo);
                UserInfo userInfo = userInfoApi.findById(video.getUserId());
                if (userInfo != null) {
                    BeanUtils.copyProperties(userInfo, videoVo);
                }
                if (video.getPicUrl() != null) {
                    videoVo.setCover(video.getPicUrl());
                }
                videoVo.setId(video.getId().toString());

                if (video.getText() != null) {
                    videoVo.setSignature(video.getText());
                }
                videoVo.setHasLiked(0);
                videoVos.add(videoVo);
            }
        }
        pageResult.setItems(videoVos);
        return ResponseEntity.ok(pageResult);
    }
}

